package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.Greedy;

/**
 * 任意次买卖
 * see 123 188
 *
 * @author tzp
 * @since 2020/9/18
 */
public class LC122 implements Greedy {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) return 0;
        int currentMin = prices[0], currentMax = prices[0], sum = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] <= currentMax) {
                sum += currentMax - currentMin;
                currentMin = prices[i];
                currentMax = prices[i];
            } else {
                currentMax = prices[i];
                if (i == prices.length - 1) {
                    sum += currentMax - currentMin;
                }
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(new LC122().maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
        System.out.println(new LC122().maxProfit(new int[]{7, 6, 5, 4}));
        System.out.println(new LC122().maxProfit(new int[]{7, 6, 5, 4, 5}));
    }
}
